/*
$("#payShare").on("tap",function () {
        //调用微信支付接口，调用成功...
        location.href = './pay_success.html';
})*/
var order_id = null;
var timer = null;
$(document).ready(function () {
    var arg = location.search;
    order_id = arg.match(/order_id=(\S*)/)[1];
    var queryPath = "order/query";
    var queryParams = {
        "SESSION_ID":SESSION_ID,
        "ORDER_ID":order_id
    }
    request(queryPath,queryParams,function (res1) {
        var name = res1.BODY.STORE_ORDER[0].SKU_LIST[0].SKU_NAME;
        var ORDER_ID = res1.BODY.ORDER_ID;
        var pay_fee = res1.BODY.PAY_FEE;
        $(".pay-coffe-name").text(name);
        $("#order").text(order_id);
        // $("#settlement_price").text(pay_fee);
        $("#total_price").text(pay_fee);
        $("#payShare").text("确认支付"+pay_fee+"元");
    });
    var remainTime = 900;
    timer = setInterval(function () {
        var minute = parseInt(remainTime/60);
        var second = remainTime%60;
        second = (second < 10) ? ("0" + second) : second
        remainTime--;
        if(minute > 0) {
            $("#remianTime").text(minute + ":" + second);
        } else {
            $("#remianTime").text("00:" + second);
            if(minute == 0) clearInterval(timer);
            }

    },1000)
})
function RegBetween(str,a,b) {
    var reg =new RegExp(a + "(\\S*)" + b);
    return str.match(reg)[1]
}
//分享咖啡支付
$("#payShare").on("tap",function () {
    var payPath = "order/payment";
    var payParams = {
        "SESSION_ID":SESSION_ID,
        "ORDER_ID":order_id,
        "PAYMENT":"WXJSPAY"
    };
    request(payPath,payParams,goTOPay)
});
function goTOPay(res) {
    var flg = /^0+$/ig.test(res.RSPCD); //全零true
    if(!flg) return showErroMsg("交易失败,返回首页！",function () {
        window.location.replace("./index.html");
    });
    var appId = RegBetween(res.BODY.PAYMENT_STR,"appId=","&timeStamp=");
    var timestamp = RegBetween(res.BODY.PAYMENT_STR,"timeStamp=","&signType=");
    var nonceStr = RegBetween(res.BODY.PAYMENT_STR,"nonceStr=","&paySign=");
    var package = RegBetween(res.BODY.PAYMENT_STR,"package=","&nonceStr=");
    var signType = RegBetween(res.BODY.PAYMENT_STR,"signType=","&package=");
    var paySign = RegBetween(res.BODY.PAYMENT_STR,"paySign=","");
    onBridgeReady(appId,timestamp,nonceStr,package,signType,paySign,function (res1) {
        location.replace("./pay_success.html?order_id="+res.BODY.ORDER_ID);
    });
    // location.href = "./order_pay.html";
}

